package com.ruibang.glass.etl.handler.sy;


import com.gitee.Jmysy.binlog4j.core.BinlogEvent;
import com.gitee.Jmysy.binlog4j.core.IBinlogEventHandler;
import com.gitee.Jmysy.binlog4j.springboot.starter.annotation.BinlogSubscriber;
import com.ruibang.glass.etl.constant.RocketMQConstants;
import com.ruibang.glass.etl.entity.BcADefectData;
import com.teaming.cloud.framework2.common.tools.JsonUtils;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.support.MessageBuilder;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

//@BinlogSubscriber(clientName = "hot-slave")
public class SYDefectDataBinlogEventHandler implements IBinlogEventHandler <BcADefectData>{


    @Autowired
    private RocketMQTemplate rocketMqTemplate;

    @Override
    public void onInsert(BinlogEvent<BcADefectData> event) {
        if(null != event.getData()){
            BcADefectData bcADefectData = event.getData();
            event.getData().setTiltedSize(bcADefectData.getF1() > bcADefectData.getF2() ? bcADefectData.getF1():bcADefectData.getF2());
            rocketMqTemplate.syncSend(RocketMQConstants.DEFECT_TOPIC, MessageBuilder.withPayload(event).build(), 3000, 15);
        }
    }

    @Override
    public void onUpdate(BinlogEvent<BcADefectData> event)  {

    }

    @Override
    public void onDelete(BinlogEvent<BcADefectData> event) {

    }

    @Override
    public boolean isHandle(String database, String table) {
        return database.startsWith("p2_") && table.equals("defectdata");
    }
}
